package com.app.studyroom.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.app.studyroom.R;
import com.app.studyroom.api.model.message.LearningRankingItem;

import java.util.List;

public class LearningRankingAdapter extends RecyclerView.Adapter<LearningRankingAdapter.ViewHolder> {

    private List<LearningRankingItem> data;

    public LearningRankingAdapter(List<LearningRankingItem> data) {
        this.data = data;
    }

    public void updateData(List<LearningRankingItem> newData) {
        this.data = newData;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public LearningRankingAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_learning_ranking, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull LearningRankingAdapter.ViewHolder holder, int position) {
        LearningRankingItem item = data.get(position);
        holder.tvName.setText(item.getUserName());
        holder.tvTime.setText(item.getStudyTime() + " 小时");
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvName, tvTime;

        ViewHolder(View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tvName);
            tvTime = itemView.findViewById(R.id.tvTime);
        }
    }
}
